/*
 * Copyright (c) 2011, Marc-Andre Ferland - madrang@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the The Warrent Team nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *     * This software is free for non-commercial use. You may not use this
 *       software, in whole or in part, in support of any commercial product
 *       without the express consent of the author.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE WARRENT TEAM BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Runtime.InteropServices;

namespace OsHelper.UsbNet.Descriptors
{
	/// <summary>
	/// Standard USB descriptor types.
	/// </summary>
	public enum StandardDescriptorSubType : byte
	{
		/// <summary>
		/// Device descriptor type.
		/// </summary>
		Device = 0x01,
		
		/// <summary>
		/// Configuration descriptor type.
		/// </summary>
		Configuration = 0x02,
		
		/// <summary>
		/// String descriptor type.
		/// </summary>
		String = 0x03,
		
		/// <summary>
		/// Interface descriptor type.
		/// </summary>
		Interface = 0x04,
		
		/// <summary>
		/// Endpoint descriptor type.
		/// </summary>
		Endpoint = 0x05,
		
		/// <summary>
		/// Device Qualifier descriptor type.
		/// </summary>
		DeviceQualifier = 0x06,
		
		/// <summary>
		/// Other Speed Configuration descriptor type.
		/// </summary>
		OtherSpeedConfiguration = 0x07,
		
		/// <summary>
		/// Interface Power descriptor type.
		/// </summary>
		InterfacePower = 0x08,
		
		/// <summary>
		/// OTG descriptor type.
		/// </summary>
		OTG = 0x09,
		
		/// <summary>
		/// Debug descriptor type.
		/// </summary>
		Debug = 0x0A,
		
		/// <summary>
		/// Interface Association descriptor type.
		/// </summary>
		InterfaceAssociation = 0x0B,
	}
	
	public enum ClassDescriptorSubType : byte
	{
		/// <summary>
		/// HID descriptor
		/// </summary>
		HID = 0x01,
		
		/// <summary>
		/// HID report descriptor
		/// </summary>
		HidReport = 0x02,
		
		/// <summary>
		/// Physical descriptor
		/// </summary>
		Physical = 0x03,
		
		/// <summary>
		/// Hub descriptor
		/// </summary>
		Hub = 0x09,
	}
	
	public enum DescriptorBaseType : byte
	{
		Standard = 0,
		Class = 1,
		Vendor = 2,
		Reserved = 3
	}
	
	[StructLayout(LayoutKind.Explicit, Size = 1)]
	public struct DescriptorType
	{
		/*
		 * Bits specifying characteristics of Descriptor Type:
		 * Bit 7 Reserved (should always be 0)
		 * Bits [6, 5] Type:
		 * Standard = 0
		 * Class = 1
		 * Vendor = 2
		 * Reserved = 3
		 * Bits [4, 0] Descriptor:
		 * See the standard class or vendor Descriptor Types table.
		 * */
		[FieldOffset(0)]
		private byte by_Value;
		
		public DescriptorType (DescriptorBaseType BaseType, byte DescValue)
		{
			this.Type = BaseType;
			this.SubType = DescValue;
		}
		
		public DescriptorType (byte Value)
		{
			this.Type = DescriptorBaseType.Vendor;
			this.SubType = Value;
		}
		
		public DescriptorType (StandardDescriptorSubType MyType)
		{
			this.Type = DescriptorBaseType.Standard;
			this.SubType = (byte)MyType;
		}
		
		public DescriptorType (ClassDescriptorSubType MyType)
		{
			this.Type = DescriptorBaseType.Class;
			this.SubType = (byte)MyType;
		}
		
		public static explicit operator byte(DescriptorType a)
		{
			return a.by_Value;
		}
		
		public static explicit operator DescriptorType(byte a)
		{
			DescriptorType DescType = new DescriptorType();
			DescType.by_Value = a;
			return DescType;
		}
		
		public override string ToString ()
		{
			if(this.Type == DescriptorBaseType.Standard)
				return ((StandardDescriptorSubType)this.SubType).ToString();
			else if (this.Type == DescriptorBaseType.Class)
				return ((ClassDescriptorSubType)this.SubType).ToString();
			else
				return string.Format("Type: {0}, Value: {1}", this.Type, this.SubType);
		}
		
		public DescriptorBaseType Type {
			get {
				return (DescriptorBaseType)(7 & (this.by_Value >> 5));
			}
			set {
				if(((byte)value & ~7) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.by_Value |= (byte)(((byte)value & 7) << 5);
			}
		}
		
		public byte SubType {
			get {
				return (byte)(this.by_Value & 31);
			}
			set {
				if((value & ~31) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.by_Value |= (byte)value;
			}
		}
		
		#region Method
		
		/// <summary>
        /// Compares a <see cref="DescriptorType"/> with this one.
        /// </summary>
        /// <param name="other">The other <see cref="DescriptorType"/>.</param>
        /// <returns>True if the Value are equal.</returns>
        public bool Equals(DescriptorType other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return this.by_Value == other.by_Value;
        }
		
		/// <summary>
		/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
		/// </summary>
		/// <returns>
		/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
		public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (DescriptorType)) return false;
            return Equals((DescriptorType)obj);
        }
		
		///<summary>
        /// <c>true</c> if the <see cref="DescriptorType"/> types are equal.
        ///</summary>
        /// <remarks>
        /// <see cref="DescriptorType"/> types are considered equal they have the same Value/>.
        /// </remarks>
        ///<param name="left"><see cref="DescriptorType"/> on the left.</param>
        ///<param name="right"><see cref="DescriptorType"/> on the right.</param>
        ///<returns>True if the <see cref="DescriptorType"/> types are equal.</returns>
        public static bool operator ==(DescriptorType left, DescriptorType right)
		{
			return Equals(left, right);
		}

        ///<summary>
        /// <c>true</c> if the <see cref="DescriptorType"/> types are not equal.
        ///</summary>
        /// <remarks>
        /// <see cref="DescriptorType"/> types are considered equal they have the same Value
		/// </remarks>
        ///<param name="left"><see cref="DescriptorType"/> on the left.</param>
        ///<param name="right"><see cref="DescriptorType"/> on the right.</param>
        ///<returns>True if the <see cref="DescriptorType"/> types are not equal.</returns>
        public static bool operator !=(DescriptorType left, DescriptorType right)
		{
			return !Equals(left, right);
		}
		
		/// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
			return this.by_Value.GetHashCode();
		}
		
		public DescriptorType Copy()
		{
			DescriptorType DescType = new DescriptorType();
			DescType.by_Value = this.by_Value;
			return DescType;
		}
		
		#endregion
		
	}
}
